using System;
using System.Text;
using System.Windows.Forms;
using facebook.Schema;

namespace facebook.desktop
{
	public partial class ProfileListItem : BaseControl
	{
		private ProfileListItem()
		{
			InitializeComponent();
		}

		public ProfileListItem(user user)
			: this()
		{
			User = user;
			LoadUser();
		}

		public user User { get; private set; }

		public event EventHandler<ProfileItemSelectedEventArgs> ProfileItemSelected;

		private void LoadUser()
		{
			if (User.pic != null)
			{
				pbProfilePicture.ImageLocation = User.pic;
			}
			else
			{
				pbProfilePicture.Image = User.picture;
			}
			lblName.Text = User.name;
			lblNetworks.Text = DisplayNetworks();
		}

		private string DisplayNetworks()
		{
			var networkList = new StringBuilder();

			foreach (var affiliation in User.affiliations.affiliation)
			{
				networkList.AppendLine(affiliation.name);
			}

			return networkList.ToString();
		}

		private void lblName_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
			if (ProfileItemSelected != null)
				ProfileItemSelected(this, new ProfileItemSelectedEventArgs(User));
		}
	}

	public class ProfileItemSelectedEventArgs : EventArgs
	{
		public ProfileItemSelectedEventArgs(user user)
		{
			User = user;
		}

		public user User { get; set; }
	}
}